from pyzbar import pyzbar
import datetime
import cv2

def detection_QR_code(img):
    # imagePath = "QR_code.png"
    # img = cv2.imread(imagePath)
    im = cv2.resize(img, (640, 520), )
    barcodes = pyzbar.decode(im)

    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 0, 255), 2)
        # 对二维码内容进行解码，输入时间，内容和类型
        barcodeData = barcode.data.decode("utf-8")
        return barcodeData
if __name__ == '__main__':
    # frame = cv2.imread('QR_code.png')
    frame = cv2.imread('285.jpg')
    barcodeData = detection_QR_code(frame)
    print(barcodeData)
        # barcodeType = barcode.type
        # text = "{}({}){}".format(datetime.datetime.now(), barcodeData, barcodeType)
        # print(text)
        # cv2.putText(im, text, (x, y - 1), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 0), 1)
        # cv2.imshow("QR code", im)
        # cv2.waitKey(0)